<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com.com>
// +----------------------------------------------------------------------
namespace app\fowl\model;

use app\admin\model\RouteModel;
use think\Model;
use tree\Tree;

class ImsRegionsModel extends Model
{

    /**
     * 查询地址树
     * @param string $region_pid  父级ID
     * @return array
     */
    public function regionTree($region_pid=1){
        $oneRegions = $this->where(['parent_id'=>$region_pid])->field('region_id,region_name,parent_id')->select()->toArray();

        $regionTree['oneRegion'] = $oneRegions;
        $twoRegion = array();
        $threeRegion = array();
        foreach($oneRegions as $k => $v){
            $arr = $this ->where(['parent_id'=>$v['region_id']])->field('region_id,region_name,parent_id')->select()->toArray();
            foreach($arr as $key => $val){
                $twoRegion[$v['region_id']][] = $val;
            }
        }
        foreach($twoRegion as $k => $v){
            foreach($v as $key => $value){
                $arr = $this ->where(['parent_id'=>$value['region_id']])->field('region_id,region_name,parent_id')->select()->toArray();
                foreach($arr as $key => $val){
                    $threeRegion[$value['region_id']][] = $val;
                }
            }
        }
        $regionTree['twoRegion'] = $twoRegion;
        $regionTree['threeRegion'] = $threeRegion;
        return $regionTree;
    }

    /**
     * 查询地址列表
     * @param string $region_pid  父级ID
     * @return array
     */
    public function regionList($region_pid=1){
        $regions = $this->where(['parent_id'=>$region_pid])->field('region_id,region_name,parent_id')->select()->toArray();

        return $regions;
    }
}